package com.qd.p2p.web.action.borrow;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.BorrowCollectionService;
import com.qdlc.p2p.biz.service.BorrowTenderHisService;
import com.qdlc.p2p.biz.service.BorrowTenderService;
import com.qdlc.p2p.biz.service.UserAutoTenderLogService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.Page;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserCache;
import com.qdlc.p2p.dal.model.BorrowCollectionModel;
import com.qdlc.p2p.dal.model.BorrowTenderHisModel;
import com.qdlc.p2p.dal.model.BorrowTenderModel;
import com.qdlc.p2p.dal.model.UserAutoTenderLogModel;
import com.qdlc.p2p.dal.util.UserUtils;

/**
 * 我的投资
 * 
 * @author cx
 * @version 2.0
 * @since 2014年3月17日
 */
public class MyInvestAction extends BaseAction<BorrowTenderModel> implements ModelDriven<BorrowTenderModel> {

	@Resource
	private BorrowCollectionService borrowCollectionService;
	@Resource
	private BorrowTenderService borrowTenderService;
	@Resource
	private BorrowTenderHisService borrowTenderHisService;
	@Resource
	private UserAutoTenderLogService userAutoTenderLogService;
	
	private User user;
	private Map<String, Object> data;

	/**
	 * 我的投资
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/member/invest/mine")
	public String mine() throws Exception {
		return "mine";
	}

	/**
	 * 我的投资列表 ajax数据接口 status
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/member/invest/mineList")
	public void mineList() throws Exception {
		user = getSessionUser();
		model.setUser(user);
		PageDataList<BorrowTenderModel> pageDataList = borrowTenderService.list(model);
		data = new HashMap<String, Object>();
		data.put("data", pageDataList);
		printWebJson(getStringOfJpaObj(data));
	}

	/**
	 * 收款明细
	 * 
	 * @return
	 */
	@Action("/member/invest/collection")
	public String collection() throws Exception {
		
		return "collection";
	}

	/**
	 * 我的待收列表 ajax数据接口 status=0待收 1已收 start_time end_time
	 * 
	 * @return
	 */
	@Action("/member/invest/collectionList")
	public void collectionList() throws Exception {
		user = getSessionUser();
		BorrowCollectionModel m = (BorrowCollectionModel) paramModel(BorrowCollectionModel.class);
		int status = paramInt("status");
		m.setStatus(status);
		m.setUser(user);
		m.setPage(model.getPage());
		PageDataList<BorrowCollectionModel> pageDataList = borrowCollectionService.list(m);
		data = new HashMap<String, Object>();
		data.put("data", pageDataList);
		printWebJson(getStringOfJpaObj(data));
	}

	/**
	 * 用户投资的借款标
	 * @return
	 */
	@Action(value="/member/invest/myInvest",results = { @Result(name = "myInvest", type = "ftl", location = "/member/invest/myInvest.html"),
			@Result(name = "myInvest_firm", type = "ftl", location = "/member_borrow/invest/myInvest.html")})
	public String myInvest() throws Exception {
		/*checkInvestUser();*/
		UserCache userCache = UserUtils.getUserCacheByUserId(getSessionUser());
		if (userCache.getUserType() == 3 || userCache.getUserType() == 2) {
			return "myInvest_firm";
		}
		request.setAttribute("isImp", userCache.getIsImp());
		return "myInvest";
	}

	/**
	 * 用户投资记录
	 * @return
	 */
	@Action("/member/invest/investRecord")
	public String investRecord() throws Exception {
		checkInvestUser();
		return "investRecord";
	}
	
	/**
	 * 自动投标记录
	 * @return
	 * @throws Exception
	 */
	@Action("/member/invest/myAutoTenderLog")
	public String myAutoTenderLog() throws Exception {
		checkInvestUser();
		return "myAutoTenderLog";
	}
	
	/**
	 * 自动投标记录列表
	 * @throws Exception
	 */
	@Action("/member/invest/myAutoTenderLogList")
	public void myAutoTenderLogList() throws Exception {
		UserAutoTenderLogModel model = new UserAutoTenderLogModel();
		model.setUserName(getSessionUserName());
		int page = paramInt("page");
		int pageNumber = page == 0 ? 1 : page;
		int pageSize = Page.ROWS;
		PageDataList<UserAutoTenderLogModel> pageDataList = userAutoTenderLogService.getUserAutoTenderLogList(pageNumber, pageSize, model);
		data = new HashMap<String, Object>();
		data.put("data", pageDataList);
		printWebJson(getStringOfJpaObj(data));
	}
	
	
	/**
	 * 用户投资的借款标列表
	 * @return
	 */
	@Action("/member/invest/myInvestList")
	public void myInvestList() throws Exception {
		model.setUserId(getSessionUserId());
		model.setSize(5);
		PageDataList<BorrowTenderModel> pageDataList = borrowTenderService.getBorrowTenderlist(model);
		data = new HashMap<String, Object>();
		data.put("data", pageDataList);
		data.put("url", SystemConfigHelper.getValue(Nid.IMAGE_SERVER_URL));
		printWebJson(getStringOfJpaObj(data));
	}
	/**
	 * 回款计划页面
	 * @return
	 */
	@Action("/member/invest/repayPlanPage")
	public String repayPlanPage() throws Exception {
		checkInvestUser();
		return "repayPlanPage";
	}
	
	/**
	 * 根据标ID获取借款标还款计划
	 * @return
	 */
	@Action("/member/invest/repayPlan")
	public void repayPlan() throws Exception {
		model.setUser(getSessionUser());
		PageDataList<BorrowCollectionModel> list = borrowCollectionService.getCurrentRepayPlanByModel(model);
		data = new HashMap<String, Object>();
		data.put("data", list);
		printWebJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 借款标回款计划
	 * @return
	 */
	@Action("/member/invest/allRepayPlan")
	public void allRepayPlan() throws Exception {
		model.setUser(getSessionUser());
		PageDataList<BorrowCollectionModel> list = borrowCollectionService.getRepayPlanByModel(model);
		data = new HashMap<String, Object>();
		data.put("data", list);
		printWebJson(getStringOfJpaObj(data));
	}
	/** 
	 * 查找当前用户投资记录
	 * @return
	 */
	@Action("/member/invest/tenderList")
	public void tenderList() throws Exception {
		model.setUser(getSessionUser());
		model.setSize(5);
		PageDataList<BorrowTenderModel> pageDataList = borrowTenderService.getTenderRecordlist(model);
		data = new HashMap<String, Object>();
		data.put("data", pageDataList);
		data.put("status", model.getStatus());
		printWebJson(getStringOfJpaObj(data));
	}

	/**
	 * 历史投资记录
	 * @return
	 */
	@Action(value="/member/invest/tender_his",results = { @Result(name = "myInvest", type = "ftl", location = "/member/invest/myInvest.html"),
			@Result(name = "myInvest_firm", type = "ftl", location = "/member_borrow/invest/myInvest.html"),
			@Result(name = "tender_his", type = "ftl", location = "/member/invest/tender_his.html")})
	public String tenderHis() throws Exception {
		/*checkInvestUser();*/
		UserCache userCache = UserUtils.getUserCacheByUserId(getSessionUser());
		if (userCache.getUserType() == 3 || userCache.getUserType() == 2) {
			return "myInvest_firm";
		}
		if(userCache.getIsImp()==0)
			return "myInvest";
		return "tender_his";
	}

	/**
	 * 查找当前用户历史投资记录
	 * @return
	 */
	@Action("/member/invest/tenderHisList")
	public void tenderHisList() throws Exception {
		model.setUser(getSessionUser());
		model.setSize(5);
		PageDataList<BorrowTenderHisModel> pageDataList = borrowTenderHisService.getTenderRecordlist(model);
		data = new HashMap<String, Object>();
		data.put("data", pageDataList);
		data.put("status", model.getStatus());
		printWebJson(getStringOfJpaObj(data));
	}
}
